﻿using System;
using FirebirdSql.Data.FirebirdClient;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Data;

namespace RMSM
{
    [Serializable()]
    public class LoaiTrangPhucBOList : Csla.NameValueListBase<int?, string>
    {
        private static string whereDefaultLoaiTrangPhucBOList = "";

        #region Factory Methods
        private LoaiTrangPhucBOList()
        {
        }

        public void InsertCustomValue(int? customKey, string customValue)
        {
            IsReadOnly = false;
            this.Insert(0, new NameValuePair(customKey, customValue));
            IsReadOnly = true;
        }

        public void AddCustomValue(int? customKey, string customValue)
        {
            IsReadOnly = false;
            this.Add(new NameValuePair(customKey, customValue));
            IsReadOnly = true;
        }

        public void CleaAll()
        {
            IsReadOnly = false;
            this.ClearItems();
            IsReadOnly = true;
        }

        private static LoaiTrangPhucBOList _list;

        public static LoaiTrangPhucBOList GetLoaiTrangPhucBOList()
        {
            return GetLoaiTrangPhucBOList(null, null, null, false);
        }

        public static LoaiTrangPhucBOList GetLoaiTrangPhucBOListWithNullValue()
        {
            return GetLoaiTrangPhucBOListWithNullValue(null);
        }

        public static LoaiTrangPhucBOList GetLoaiTrangPhucBOListWithNullValue(string where)
        {
            _list = GetLoaiTrangPhucBOList(where, null, null, false);
            _list.IsReadOnly = false;
            _list.Insert(0, new NameValuePair(null, " "));
            _list.IsReadOnly = true;
            return _list;
        }

        public static LoaiTrangPhucBOList GetLoaiTrangPhucBOList(bool getDeleted)
        {
            return GetLoaiTrangPhucBOList(null, null, null, getDeleted);
        }

        public static LoaiTrangPhucBOList GetLoaiTrangPhucBOList(string where)
        {
            return GetLoaiTrangPhucBOList(where, null, null, false);
        }

        public static LoaiTrangPhucBOList GetLoaiTrangPhucBOList(string where, bool getDeleted)
        {
            return GetLoaiTrangPhucBOList(where, null, null, getDeleted);
        }

        public static LoaiTrangPhucBOList GetLoaiTrangPhucBOList(string where, string orderCol)
        {
            return GetLoaiTrangPhucBOList(where, orderCol, null, false);
        }

        public static LoaiTrangPhucBOList GetLoaiTrangPhucBOList(string where, string orderCol, bool getDeleted)
        {
            return GetLoaiTrangPhucBOList(where, orderCol, null, getDeleted);
        }

        public static LoaiTrangPhucBOList GetLoaiTrangPhucBOList(string where, string orderCol, string orderExp)
        {
            return GetLoaiTrangPhucBOList(where, orderCol, orderExp, false);
        }

        public static LoaiTrangPhucBOList GetLoaiTrangPhucBOList(string where, string orderCol, string orderExp, bool getDeleted)
        {
            string swhere = "";
            if (getDeleted == false)
            {
                swhere = "WHERE DELE='0'";

                if (string.IsNullOrEmpty(whereDefaultLoaiTrangPhucBOList) == false)
                    swhere += " AND " + whereDefaultLoaiTrangPhucBOList;

                if (string.IsNullOrEmpty(where) == false)
                    swhere += " AND " + where;
            }
            else
            {
                if (string.IsNullOrEmpty(whereDefaultLoaiTrangPhucBOList) == false)
                    swhere = "WHERE " + whereDefaultLoaiTrangPhucBOList;

                if (string.IsNullOrEmpty(where) == false)
                    swhere += (swhere == "" ? "" : " AND ") + where;
            }

            SPParam spParam = new SPParam((string.IsNullOrEmpty(orderCol) ? "TEN" : orderCol), (string.IsNullOrEmpty(orderExp) ? "ASC" : orderExp), swhere);
            _list = DataPortal.Fetch<LoaiTrangPhucBOList>(spParam);
            return _list;
        }

        public static void InvalidateCache()
        {
            _list = null;
        }
        #endregion Factory Methods
        //==========================================================================

        #region Data Access
        protected override void DataPortal_Fetch(object _spParam)
        {
            RaiseListChangedEvents = false;
            SPParam spParam = (SPParam)_spParam;
            using (FbConnection conn = new FbConnection(Session.CnnStr))
            {
                conn.Open();
                using (FbCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "LoaiTrangPhucBOList";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@WHEREEXP", FbDbType.VarChar).Value = spParam.whereExpression;
                    cmd.Parameters.Add("@ORDERCOL", FbDbType.VarChar).Value = spParam.orderColumn;
                    cmd.Parameters.Add("@ORDEREXP", FbDbType.VarChar).Value = spParam.orderExpression;

                    using (FbDataReader reader = cmd.ExecuteReader())
                    {
                        IsReadOnly = false;
                        string Value = null;

                        while (reader.Read())
                        {
                            Value = null;
                            if (!(reader["TEN"] is DBNull))
                                Value += (string)reader["TEN"];

                            this.Add(new NameValuePair((int)reader["LoaiTrangPhucID"], Value));
                        }
                        IsReadOnly = true;
                    }
                }
            }
            RaiseListChangedEvents = true;
        }
        #endregion Data Access
        //==========================================================================
    }
}